﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MMSWebsite.DataAccessManagement;
using MMSWebsite.Models;
using System.Transactions;
using MMSWebsite.Models.Asset;

namespace MMSWebsite.Controllers
{
    //[HandleError(Exception = typeof(AppException), View = "ApplicationError")]
    //[HandleError(ExceptionType = typeof(DbException), View = "DatabaseError")]
    [HandleError(View = "Error")]
    public class MasterMgnController : Controller
    {
        MasterDataAccessProvider mdap = null;

        #region INDEX, LIST VIEW
        /// <summary>
        /// Get List Of Master Data Informations
        /// Vert Method: GET
        /// Url template: /MasterMgn/
        /// </summary>
        /// <returns>List View</returns>
        public ActionResult Index()
        {
            try
            {
                mdap = new MasterDataAccessProvider();
                List<MasterData> lsMd = mdap.GetMDList();
                return View(lsMd);
            }
            catch (Exception ex)
            {
                // To do: return error view or notify message
                return View();
            }
        }
        #endregion INDEX, LIST VIEW

        #region CREATE VIEW
        /// <summary>
        /// Direct to create view
        /// Verb method: GET
        /// Url template: /MasterMgn/Create
        /// </summary>
        /// <returns>Create view</returns>
        public ActionResult Create()
        {
            return View("Create");
        }

        /// <summary>
        /// Create new master data. If create succesfully, redirect to index page. 
        /// If not, notify message
        /// Vert method: POST
        /// Url template: /MasterMgn/Create...
        /// </summary>
        /// <param name="md">Master Data Information</param>
        /// <returns>If create succesfully, redirect to index page. 
        /// If not, notify message</returns>
        [HttpPost]
        public ActionResult Create(MasterData md)
        {
            bool isSuccess = false;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    mdap = new MasterDataAccessProvider();
                    isSuccess = mdap.AddMasterData(md);
                    if (isSuccess)
                    {
                        ts.Complete();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return View();
                    }

                }
                catch (Exception ex)
                {
                    // To do: show message, write log
                    return View();
                }
            }
        }
        #endregion CREATE VIEW

        /// <summary>
        /// Get Detail Of Master Data By Id
        /// <para>Verb Method: GET</para>
        /// <para>Uri template: /MasterMgn/Details/5</para>
        /// </summary>
        /// <param name="id">Master Data Id</param>
        /// <returns></returns>
        public ActionResult Details(int id)
        {
            try
            {
                mdap = new MasterDataAccessProvider();
                MasterData md = mdap.GetMDById(id);
                if (md != null)
                {
                    return View("Details", md);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                // To do: show message, write log
                return RedirectToAction("Index");
            }
        }

        /// <summary>
        /// Edit Master Data
        /// <para>Verb Method: GET</para>
        /// <para>Uri Template: /MasterMgn/Edit/5</para>
        /// </summary>
        /// <param name="id">Master Data Id</param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            try
            {
                mdap = new MasterDataAccessProvider();
                MasterData md = mdap.GetMDById(id);
                if (md != null)
                {
                    return View("Edit", md);
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                // To do: show message, write log
                return RedirectToAction("Index");
            }
        }

        /// <summary>
        /// Edit Master Data
        /// <para>Verb Method: POST</para>
        /// <para>Uri Template: /MasterMgn/Edit/5</para>
        /// </summary>
        /// <param name="id">Master Data Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(int id, MasterData md)
        {
            bool isSuccess = false;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    mdap = new MasterDataAccessProvider();
                    isSuccess = mdap.UpdateMasterData(md);
                    if (isSuccess)
                    {
                        ts.Complete();
                        return View("Details", md);
                    }
                    else
                    {
                        return RedirectToAction("Index");
                    }
                }
                catch (Exception ex)
                {
                    // To do: show message, write log
                    return RedirectToAction("Index");
                }
            }
        }

        /// <summary>
        /// Edit Master Data
        /// <para>Verb Method: GET</para>
        /// <para>Uri Template: /MasterMgn/Delete/5</para>
        /// </summary>
        /// <param name="id">Master Data Id</param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            bool isSuccess = false;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
            {
                try
                {
                    mdap = new MasterDataAccessProvider();

                    isSuccess = mdap.DeleteMasterData(id);
                    if (isSuccess)
                    {
                        ts.Complete();
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return RedirectToAction("Index");
                    }
                }
                catch (Exception ex)
                {
                    // To do: show message, write log
                    return RedirectToAction("Index");
                }
            }
        }
    }
}
